

//
//  WalletListViewController.m
//  Luxury
//
//  Created by DCQ on 2018/5/21.
//  Copyright © 2018年 ZNXZ. All rights reserved.
//

#import "WalletListViewController.h"
#import "WalletIncomeCell.h"
#import "WalletSpendingCell.h"
#import "UserInfoModel.h"
#import "WalletListModel.h"

NSString * const WalletIncomeCellIdentifier = @"WalletIncomeCellIdentifier";
NSString * const WalletSpendingCellIdentifier = @"WalletSpendingCellIdentifier";
@class XLPagerTabStripViewController;
@interface WalletListViewController ()
@property(nonatomic,assign) TableViewControllerStyle style;
@property(nonatomic,strong) WalletListModel * model;
@end

@implementation WalletListViewController

- (instancetype)initWithStyle:(TableViewControllerStyle)style{
    self = [super init];
    if (self) {
        self.style = style;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
}
- (BOOL)isAddRefreshHeader{
    return YES;
}
- (BOOL)isAddRefreshFooter{
    return YES;
}
- (void)requestWalletUseLog{
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params addEntriesFromDictionary:@{@"token":UserInfo.userToken}];
   
    NSString * type = @"";//0-收入 1-支出
    if (self.style == TableViewControllerStyle_income) {
        type = @"0";
    }else if (self.style == TableViewControllerStyle_spending){
        type = @"1";
    }
    [params addEntriesFromDictionary:@{@"type":type}];
    [params addEntriesFromDictionary:@{@"p":@(self.currentPageNo)}];
    [params addEntriesFromDictionary:@{@"psize":@(self.pageCount)}];
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Wallet/getUseLog" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (strongSelf.currentPageNo == 0) {
            strongSelf.model = nil;
            strongSelf.model = [[WalletListModel alloc]initWithDictionary:responseObject error:nil];
            strongSelf.currentPageNo ++;
            
        }else{
            //
            if (responseObject[@"data"] && [responseObject[@"data"] count] > 0) {
                 [strongSelf.model.data addObjectsFromArray: responseObject[@"data"] ];
                 strongSelf.currentPageNo ++;
            }
            
        }
        [strongSelf updateTableView];
    }];
}
- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WalletIncomeCell class]) bundle:nil] forCellReuseIdentifier:WalletIncomeCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WalletSpendingCell class]) bundle:nil] forCellReuseIdentifier:WalletSpendingCellIdentifier];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSString *)titleForPagerTabStripViewController:(XLPagerTabStripViewController *)pagerTabStripViewController{
    NSString * tabTitle = @"";
    switch (self.style) {
        case TableViewControllerStyle_income:
            tabTitle = @"收入";
            break;
        case TableViewControllerStyle_spending:
            tabTitle = @"支出";
            break;
      
        default:
            break;
    }
    return   tabTitle;
}


- (CGRect)getTableViewFrame{
    CGRect tableViewFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44 -70);
    return tableViewFrame;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rows = 0;
    rows = [self.model.data count];
    return rows;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
      WalletInfoModel * model = self.model.data[indexPath.row];
    if (self.style == TableViewControllerStyle_income) {
        WalletIncomeCell  *tempCell = [tableView dequeueReusableCellWithIdentifier:WalletIncomeCellIdentifier];
   
        [tempCell setupWalletModel:model];
        cell = tempCell;
    }else if (self.style == TableViewControllerStyle_spending){
        WalletSpendingCell  *tempCell = [tableView dequeueReusableCellWithIdentifier:WalletSpendingCellIdentifier];
        [tempCell setupWalletModel:model];
        cell = tempCell;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

//   页面加载调用 子类重写
- (void)getNormalTableViewNetworkData{
    NSLog(@"进入页面请求 子类重写");
    self.currentPageNo = 0;
    self.pageCount = 10;
 
    [self requestWalletUseLog];
}
- (void)getLoadMoreTableViewNetworkData{
    
    [self requestWalletUseLog];
}
- (NSInteger )getNetworkTableViewDataCount{
    
    return  [self.model.data count];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
